<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Object Aggregation examples</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="objaggregation.examples2.html">范例</a></div>
 <div class="up"><a href="objaggregation.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="objaggregation.examples.association" class="section">
  <h2 class="title">Object Aggregation examples</h2>
    <p class="para">
     An <em class="emphasis">Association</em> is a composition of independently constructed and
     externally visible parts.  When we associate classes or objects, each
     one keeps a reference to the ones it is associated with. When we
     associate classes statically, one class will contain a reference to an
     instance of the other class. For example:
     <div class="example" id="example-5023">
      <p><strong>Example #1 Class association</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDateTime&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;empty&nbsp;constructor<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">now</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s"</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">Report&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$_dt</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;properties&nbsp;...<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">Report</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;initialization&nbsp;code&nbsp;...<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">generateReport</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dateTime&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">now</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;code&nbsp;...<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;methods&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$rep&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Report</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
     We can also associate instances at runtime by passing a reference in a
     constructor (or any other method), which allow us to dynamically change
     the association relationship between objects. We will modify the example 
     above to illustrate this point:
     <div class="example" id="example-5024">
      <p><strong>Example #2 Object association</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDateTime&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;same&nbsp;as&nbsp;previous&nbsp;example<br /></span><span style="color: #007700">}<br /><br />class&nbsp;</span><span style="color: #0000BB">MyDateTimePlus&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$_format</span><span style="color: #007700">;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">MyDateTimePlus</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">=</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s"</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">now</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_format</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">Report&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$_dt</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;we'll&nbsp;keep&nbsp;the&nbsp;reference&nbsp;to&nbsp;MyDateTime&nbsp;here<br />&nbsp;&nbsp;//&nbsp;more&nbsp;properties&nbsp;...<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">Report</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;do&nbsp;some&nbsp;initialization<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_dt&nbsp;</span><span style="color: #007700">=&amp;&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">generateReport</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dateTime&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">now</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;code&nbsp;...<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;methods&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$rep&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Report</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$dtp&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyDateTimePlus</span><span style="color: #007700">(</span><span style="color: #DD0000">"l,&nbsp;F&nbsp;j,&nbsp;Y&nbsp;(h:i:s&nbsp;a,&nbsp;T)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;generate&nbsp;report&nbsp;with&nbsp;simple&nbsp;date&nbsp;for&nbsp;web&nbsp;display<br /></span><span style="color: #0000BB">$rep</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(</span><span style="color: #0000BB">$dt</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$rep</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateReport</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;later&nbsp;on&nbsp;in&nbsp;the&nbsp;code&nbsp;...<br /><br />//&nbsp;generate&nbsp;report&nbsp;with&nbsp;fancy&nbsp;date<br /></span><span style="color: #0000BB">$rep</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(</span><span style="color: #0000BB">$dtp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$rep</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateReport</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;$output&nbsp;in&nbsp;database<br />//&nbsp;...&nbsp;etc&nbsp;...&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     <em class="emphasis">Aggregation</em>, on the other hand, implies 
     encapsulation (hidding) of the
     parts of the composition. We can aggregate classes by using a (static)
     inner class (PHP does not yet support inner classes), in this case the
     aggregated class definition is not accessible, except through the class
     that contains it. The aggregation of instances (object aggregation)
     involves the dynamic creation of subobjects inside an object, in the
     process, expanding the properties and methods of that object.
    </p>
    <p class="para">
     Object aggregation is a natural way of representing a whole-part relationship, 
     (for example, molecules are aggregates of atoms), or can be used to
     obtain an effect equivalent to multiple inheritance, without having to
     permanently bind a subclass to two or more parent classes and their
     interfaces. In fact object aggregation can be more flexible, in which we
     can select what methods or properties to &quot;inherit&quot; in the aggregated
     object.
    </p>
   </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="objaggregation.examples2.html">范例</a></div>
 <div class="up"><a href="objaggregation.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
